package com.ms.p2p.service.impl;

import com.ms.common.exception.NoticeException;
import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.dao.LoanTenderRecordDao;
import com.ms.p2p.dao.MemberDao;
import com.ms.p2p.entity.loan.Loan;
import com.ms.p2p.entity.loan.LoanTenderRecord;
import com.ms.p2p.entity.member.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class LoanTenderRecordServiceImpl  {
	
	@Autowired
	LoanTenderRecordDao tenderDao;
	
	@Autowired MemberDao memberDao;

	public List getList(int borrowId) {
		return tenderDao.getList(borrowId);
		
	}

	public double getUserTenderSum(int loanId, int userId){
		return tenderDao.getUserTenderSum(loanId,userId);
	}


	/**
	 * 插入投标记录，增加user表的投标次数，记录初次投标时间
	 * 
	 * @param map
	 * @param max_account
	 * @param user
	 * @return
	 */
	@Transactional(readOnly = false,propagation = Propagation.REQUIRED)
	public int addTenderLog(LoanTenderRecord map, BigDecimal max_account, Member user) {
		//对于更新首次投标时间进行粗粒度控制 提升投标效率
		if(user.getFirst_tender_time()==null){
			memberDao.updateFirst(map.getUser_id()+"", MsDateUtils.getTime());
		}
		memberDao.updateTenderTimes(map.getUser_id()+"");
		if(max_account.compareTo(BigDecimal.ZERO)==1){
			return tenderDao.addTenderLog(map,max_account);
		}else{
			return tenderDao.insert(map);

		}
	}

	/**
	 * 投标时间查出一个标下的所有投标记录（投标时间倒序）
	 * 
	 * @param borrowId
	 * @return
	 */
	public List<LoanTenderRecord> getTenderLog(int borrowId) {
		return tenderDao.getTenderLog(borrowId);

	}

	public List<Map<String,Object>> findTenders(int borrowId) {
		return tenderDao.findTenders(borrowId);
	}



	public Map<String, Object> sumByBorrowId(int borrowId) {
		return tenderDao.getSum(borrowId);

	}


	public List<Map<String,Object>> findPager(Integer id, String status, PageBean page) {
		return tenderDao.findPager(id,status, page.getBegin(), page.getPageSize());

	}
	public int findCount(Integer id, String status) {
		return tenderDao.findCount(id,status);
	}

	/**
	 * 根据ID查询一条投标记录
	 */
	public LoanTenderRecord findById(int id) {
		return tenderDao.get(id+"");

	}

	public int update(LoanTenderRecord map) {
		return tenderDao.update(map);
	}

	/**
	 *  累计赚取收益
	 * @return
	 */
	public Map<String, Object> countTotalIncome() {
		return tenderDao.caculeIncome();

	}

	public String countWailtAccount() {
		return tenderDao.caculeWait();
	}

	/**
	 * 新手标判断
	 * */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void doRookie(Member user, Loan borrow) {
		Member checkUser = tenderDao.checkTender(borrow.getId(),user.getId());
		if (checkUser == null) {
			throw new NoticeException("新手标您只能投资一次");
		}
		tenderDao.doRookieTender(borrow.getId(),user.getId());
	}

	public List<Map<String,Object>> findActive() {
		return tenderDao.findActive();
	}

	public List<Map<String, Object>> findLoanPager(Integer id, String status, PageBean page) {
		return tenderDao.findLoanPager(id,status,page.getBegin(),page.getPageSize());
	}

	public int findLoanCount(Integer id, String status) {
		return tenderDao.findLoanCount(id,status);
	}
}
